package com.baidu.cmsService.dao;

import com.baidu.cmsCommes.pojo.Channel;
import com.baidu.cmsCommes.pojo.ChannelType;
import com.baidu.cmsCommes.pojo.MyTypeHandle;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/22/10:03
 */
@Mapper
public interface ChannelDao {

    @Select("select * from t_channel")
    @Results(id = "channelMap",value = {
            @Result(property = "id",javaType = Integer.class,column = "id",jdbcType = JdbcType.INTEGER,id = true),
            @Result(property = "custom_link",javaType = Integer.class,column = "custom_link",jdbcType = JdbcType.INTEGER),
            @Result(property = "custom_link_url",javaType = String.class,column = "custom_link_url",jdbcType = JdbcType.VARCHAR),
            @Result(property = "is_index",javaType = Integer.class,column = "is_index",jdbcType = JdbcType.INTEGER),
            @Result(property = "is_top_nav",javaType = Integer.class,column = "is_top_nav",jdbcType = JdbcType.INTEGER),
            @Result(property = "name",javaType = String.class,column = "name",jdbcType = JdbcType.VARCHAR),
            @Result(property = "orders",javaType = Integer.class,column = "orders",jdbcType = JdbcType.INTEGER),
            @Result(property = "status",javaType = Integer.class,column = "status",jdbcType = JdbcType.INTEGER),
            @Result(property = "type",javaType = ChannelType.class,column = "type",jdbcType = JdbcType.INTEGER,typeHandler = MyTypeHandle.class),
            @Result(property = "pid",javaType = Integer.class,column = "pid",jdbcType = JdbcType.INTEGER),
            @Result(property = "show_index",javaType = Integer.class,column = "show_index",jdbcType = JdbcType.INTEGER)
    })
    @ResultType(Channel.class)
    List<Channel> listTree();

    @Select("select * from t_channel where id = #{cid}")
    @ResultMap("channelMap")
    Channel selectById(@Param("cid") Integer cid);

    @Select("select * from t_channel where pid = #{cid}")
    @ResultMap("channelMap")
    List<Channel> selectChilds(@Param("cid") Integer cid);

    @Select("select max(orders) from t_channel where pid = #{cid}")
    Integer selectMaxOrders(@Param("cid") Integer pid);

    @Insert("insert into t_channel (id,custom_link,custom_link_url,is_index,is_top_nav,name,orders,recommend,status,type,pid,show_index) " +
            "value(#{id},#{custom_link},#{custom_link_url},#{is_index},#{is_top_nav},#{name},#{orders},#{recommend},#{status},#{type,typeHandler=com.baidu.cmsCommes.pojo.MyTypeHandle},#{pid}, #{show_index})")
    Integer saveChannel(Channel channel);

    @Update("update t_channel set custom_link=#{custom_link},custom_link_url=#{custom_link_url},is_index=#{is_index},is_top_nav=#{is_top_nav},name=#{name},orders=#{orders},recommend=#{recommend},status=#{status},type=#{type,typeHandler=com.baidu.cmsCommes.pojo.MyTypeHandle},pid=#{pid},show_index=#{show_index} where id=#{id}")
    Integer updateChannel(Channel channel);

    @Delete("delete from t_channel where id = #{id} ")
    Integer deleteChannelById(@Param("id") Integer id);

    @Delete("delete from t_channel where pid = #{id} ")
    Integer deleteChannelByPid(@Param("id") Integer id);

    @Update("update t_channel set orders=#{orders} where id=#{id}")
    Integer updateSort(@Param("id") Integer id,@Param("orders") Integer ordersNum);
}
